<?php
/**
 * File : LibraryPathFactory.php
 * Created by Ludovic Reenaers
 * Created on  21 oct. 2010
 * lreenaers@hotmail.com
 */
class LibraryPathFactory{
	private $beans;
	private $daos;
	private $generators;
	private $models;
	private $views;
	private $utils;
	private $factories;

	private $beansdir ;
	private $daodir ;
	private $gendir ;
	private $modelsdir;
	private $viewsdir;
	private $utildir ;
	private $factodir;

	function __construct($pathToSite){

		$this->beansdir = $pathToSite."/beans/";
		$this->daodir = $pathToSite."/dao/";
		$this->gendir = $pathToSite."/generators/";
		$this->modelsdir = $pathToSite."/models/";
		$this->viewsdir = $pathToSite."/views/";
		$this->utildir = $pathToSite."/util/";
		$this->factodir = $pathToSite."/factories/";

		$this->beans = $this->getContent($this->beansdir);
		$this->daos = $this->getContent($this->daodir);
		$this->generators = $this->getContent($this->gendir);
		$this->models = $this->getContent($this->modelsdir);
		$this->views = $this->getContent($this->viewsdir);
		$this->utils = $this->getContent($this->utildir);
		$this->factories =$this->getContent($this->factodir);


	}
	private function getContent($dir){
		$tabtopush = array();
		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false) {
					if(filetype($dir . $file)=="file"){
						$tab = explode('.',$file);
						if($tab[1]=="php"){
							array_push($tabtopush,$tab[0]);
						}

					}
				}
				closedir($dh);
			}
		}
		return $tabtopush;
	}
	public function getPath($libname){
		$pth=null;
		if($libname == 'Securimage'){
			$pth=$_SESSION['config']->pathtosite.'/securimage/securimage.php';
		}elseif (in_array($libname,	$this->beans)){
			$pth=$this->beansdir.$libname.".php";
		}elseif (in_array($libname,	$this->daos)){
			$pth=$this->daodir.$libname.".php";
		}elseif (in_array($libname,	$this->generators)){
			$pth=$this->gendir.$libname.".php";
		}elseif (in_array($libname,	$this->models)){
			$pth=$this->modelsdir.$libname.".php";
		}elseif (in_array($libname,	$this->views)){
			$pth=$this->viewsdir.$libname.".php";
		}elseif (in_array($libname,	$this->utils)){
			$pth=$this->utildir.$libname.".php";
		}elseif (in_array($libname,	$this->factories)){
				
			$pth=$this->factodir.$libname.".php";
		}
		return $pth;
	}
}
?>